package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.DayWork;
import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.mapper.DayWorkMapper;
import cn.wolfcode.education.mapper.DeptMapper;
import cn.wolfcode.education.mapper.EmployeeMapper;
import cn.wolfcode.education.query.DayWorkQuery;
import cn.wolfcode.education.service.IDayWorkService;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DayWorkServiceImpl implements IDayWorkService {
    @Autowired
    private DayWorkMapper dayWorkMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    public PageResult queryForList(DayWorkQuery qo) {
        //判断当前用户是否是部门管理员
        //获取当前用户
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();

        //查找所有部门管理员
        List<Long> allManagerIds = deptMapper.getAllManagerId();
        //判断是否是部门管理员
        if (allManagerIds.contains(currentUser.getId()) || currentUser.isAdmin()){
            //获取所属部门的id
            List<Long> deptIds = deptMapper.getDeptIdsByManagerId(currentUser.getId());
            //将deptids封装到qo中
            qo.setDeptIds(deptIds);
            //通过部门查找该部门的所有每日任务
            int mCount = dayWorkMapper.getDayWorksByCountDeptId(qo);
            if (mCount == 0){
                return new PageResult();
            }
            List<DayWork> mDayWorks = dayWorkMapper.getDayWorksByDeptId(qo);
            return new PageResult(mCount,mDayWorks);
        }else {
            //通过员工获取每日任务
            //将员工id封装到qo中
            qo.setEmployeeId(currentUser.getId());
            int eCount = dayWorkMapper.getDayWorksByEmployeeId(qo);
            if (eCount == 0){
                return new PageResult();
            }
            List<DayWork> eDayWorks = dayWorkMapper.getDayWorksCountByEmployeeId(qo);
            return new PageResult(eCount,eDayWorks);
        }
    }

    /**
     * 指派任务
     * @param dayWork
     */
    public void insert(DayWork dayWork) {
        //设置当前人员的所属部门
        Employee employee = employeeMapper.selectByPrimaryKey(dayWork.getEmployee().getId());
        dayWork.setDept(employee.getDept());
        dayWorkMapper.insert(dayWork);
    }

    public void update(DayWork dayWork) {
        //查看任务状态---初始状态和处理中可以修改
        DayWork work = dayWorkMapper.selectByPrimaryKey(dayWork.getId());
        if (work.getState() == DayWork.STATIC_ON || work.getState() == DayWork.STATIC_ZERO){
            dayWorkMapper.updateByPrimaryKey(dayWork);
        }else {
            throw new RuntimeException("不能被修改");
        }
    }

    public void delete(Long id) {
        dayWorkMapper.deleteByPrimaryKey(id);
    }

    public void fail(Long id) {
        DayWork work = dayWorkMapper.selectByPrimaryKey(id);
        if (work.getState() != DayWork.STATIC_ZERO){
            dayWorkMapper.updateStateFail(id);
        }else {
            throw new RuntimeException("当前状态不能修改状态");
        }
    }

    public void esave(DayWork dayWork) {
        //修改任务状态
        if (dayWork.getState() != DayWork.STATIC_FAIL){
            dayWork.setState(DayWork.STATIC_ON);
            dayWorkMapper.updateDescriptionHandle(dayWork);
        }else {
            throw new RuntimeException("当前状态不能修改状态");
        }
    }

    public void success(Long id) {
        DayWork work = dayWorkMapper.selectByPrimaryKey(id);
        if (work.getState() != DayWork.STATIC_ZERO){
            dayWorkMapper.updateStateSuccess(id);
        }else {
            throw new RuntimeException("当前状态不能修改状态");
        }
    }
}
